package com.yanqu.road.dao.impl.mail;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.DataStatus;
import com.yanqu.road.entity.mail.AllMailLanguage;
import com.yanqu.road.entity.player.ServerMail;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ServerMailDaoImpl extends TempDataObjectDao<ServerMail> {

	public Map<Integer, ServerMail> getServerMailMap() {
		Map<Integer, ServerMail> dataMap = new ConcurrentHashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_server_mail order by mailId";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				ServerMail data = parse(rs);
				dataMap.put(data.getMailId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public ServerMail addServerMail(ServerMail serverMail) {
		ResultSet rs = null;
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_server_mail(mailType,title,content,annexes,sendTime,isDelete,appIdList,massId) VALUES(?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			int i = 1;
			call.setInt(i++, serverMail.getMailType());
			call.setString(i++, serverMail.getTitle());
			call.setString(i++, serverMail.getContent());
			call.setString(i++, serverMail.getAnnexes());
			call.setTimestamp(i++, new Timestamp(serverMail.getSendTime().getTime()));
			call.setBoolean(i++, serverMail.isDelete());
			call.setString(i++, JSON.toJSONString(serverMail.getAppIdSet()));
			call.setLong(i++, serverMail.getMassId());
			call.execute();
			rs = call.getGeneratedKeys();
			if (rs != null && rs.next()) {
				serverMail.setMailId(rs.getInt(1));
				return serverMail;
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	@Override
	protected boolean addData(ServerMail data) {
		return false;
	}

	@Override
	protected boolean updateData(ServerMail data) {
		return false;
	}

	@Override
	protected ServerMail parseData(ResultSet rs) throws SQLException {
		ServerMail data = new ServerMail();
		data.setMailId(rs.getInt("mailId"));
		data.setMailType(rs.getInt("mailType"));
		data.setTitle(rs.getString("title"));
		data.setContent(rs.getString("content"));
		data.setAnnexes(rs.getString("annexes"));
		data.setSendTime(rs.getTimestamp("sendTime"));
		data.setDelete(rs.getBoolean("isDelete"));
		data.setMassId(rs.getLong("massId"));
		String appIdListStr = rs.getString("appIdList");
		if (!StringUtils.isNullOrEmpty(appIdListStr)) {
			List<String> strings = JSON.parseArray(appIdListStr, String.class);
			data.setAppIdSet(new HashSet<>(strings));
		}
		return data;
	}

	public boolean insertNewMailLanguage(AllMailLanguage allMailLanguage) {
		getDb().setConnName(DbNameStrategy.getUserDb());
		String sql = "INSERT INTO t_u_server_mail_language(id,languageType,title,msg)" + " values(?,?,?,?)";
		int i = 1;
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(i++, new DbParameter(Types.INTEGER, allMailLanguage.getMailId()));
		para.put(i++, new DbParameter(Types.VARCHAR, allMailLanguage.getLanguageType()));
		para.put(i++, new DbParameter(Types.VARCHAR, allMailLanguage.getTitle()));
		para.put(i++, new DbParameter(Types.VARCHAR, allMailLanguage.getMsg()));

		if (getDb().executeNoneQuery(sql, para) > 0) {
			allMailLanguage.setOp(DataStatus.None);
			return true;
		}
		return false;
	}

	public Map<Integer, Map<String, AllMailLanguage>> getAllMailsLanguage() {
		Map<Integer, Map<String ,AllMailLanguage>> allMailsLanguageMap = new ConcurrentHashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			String sql = "select *  from t_u_server_mail_language order by id ";
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				AllMailLanguage allMailLanguage = new AllMailLanguage();
				int id = rs.getInt("id");
				String languageType = rs.getString("languageType");
				String title = rs.getString("title");
				String msg = rs.getString("msg");
				if (!allMailsLanguageMap.containsKey(id)) {
					allMailsLanguageMap.put(id, new ConcurrentHashMap<>());
				}
				allMailLanguage.setMailId(id);
				allMailLanguage.setLanguageType(languageType);
				allMailLanguage.setTitle(title);
				allMailLanguage.setMsg(msg);
				allMailsLanguageMap.get(id).put(allMailLanguage.getLanguageType() ,allMailLanguage);
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return allMailsLanguageMap;
	}
}
